package com.pf.business.export.handler;

import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.util.BooleanUtils;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.google.common.collect.Lists;
import com.pf.utils.ExportExcelUtils;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.ss.usermodel.*;

/**
 * @author PanFei
 * @version 1.0.0
 * @createTime 2023/7/15 20:07
 */
public class BillCellWriteHandler implements CellWriteHandler {

    @Override
    public void afterCellDispose(CellWriteHandlerContext context) {

        // 判断不是表头，如果是fill的情况，这里会是null。所以用not true
        if (BooleanUtils.isNotTrue(context.getHead())) {
            Workbook workbook = context.getWriteSheetHolder().getSheet().getWorkbook();
            Sheet sheet = context.getWriteSheetHolder().getSheet();
            WriteCellData<?> firstCellData = context.getFirstCellData();
            Integer columnIndex = firstCellData.getColumnIndex();
            // columnIndex = 0 为【账单描述】列
            if (columnIndex == 0) {
                // 为第一列设置超链接
                Cell cell = context.getCell();
                CreationHelper creationHelper = workbook.getCreationHelper();
                Hyperlink hyperlink = creationHelper.createHyperlink(HyperlinkType.URL);
                hyperlink.setAddress("https://www.baidu.com?wd=" + cell.getStringCellValue());
                cell.setHyperlink(hyperlink);

                // 设置样式
                WriteCellStyle writeCellStyle = firstCellData.getOrCreateStyle();
                // 设置字体蓝色下划线
                WriteFont writeFont = new WriteFont();
                writeFont.setUnderline(Font.U_SINGLE);
                writeFont.setColor(IndexedColors.BLUE1.getIndex());
                // 灰色背景
                writeCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
                writeCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
                writeCellStyle.setWriteFont(writeFont);
            } else if (columnIndex == 2) {
                // 【账单类型】列设置下拉列表
                ExportExcelUtils.createBoundedDropDownList(sheet, columnIndex, Lists.newArrayList("收益", "支出"));
            }
            // 这样样式就设置好了 后面有个FillStyleCellWriteHandler 默认会将 WriteCellStyle 设置到 cell里面去 所以可以不用管了
        }
    }
}
